<?php

App::uses('AppModel', 'Model');
App::uses('CompositeKeyModel', 'Model');

/**
 * OrganisationGroup Model
 *
 * @property OrganisationGroupType $OrganisationGroupType
 * @property OrganisationMemberGroup $OrganisationMemberGroup
 * @property OrganisationGroupLeader $OrganisationGroupLeader 
 */
class OrganisationGroup extends CompositeKeyModel {

    //The Associations below have been created with all possible keys, those that are not needed can be removed

    public $useDbConfig = "organisations";
    public $primaryKeys = array('id', 'organisation_id');

    /**
     * belongsTo associations
     *
     * @var array
     */
    public $belongsTo = array(
        'OrganisationGroupType' => array(
            'className' => 'OrganisationGroupType',
            'foreignKey' => 'organisation_group_type_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    /**
     * hasMany associations
     *
     * @var array
     */
    public $hasMany = array(
        'OrganisationMemberGroup' => array(
            'className' => 'OrganisationMemberGroup',
            'foreignKey' => 'organisation_group_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => ''
        ),
        'OrganisationGroupLeader' => array(
            'className' => 'OrganisationGroupLeader',
            'foreignKey' => 'organisation_group_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => ''
        )
    );

    public function addLeader($organisation_id, $organisation_group_id, $organisation_member_id, $role) {
        $leader = $this->getLeader($organisation_id, $organisation_group_id, $organisation_member_id);
        
        $this->OrganisationGroupLeader->create();

        $saved = $this->OrganisationGroupLeader->save(array(
            'id' => $leader ? $leader['OrganisationGroupLeader']['id'] : null,
            'organisation_id' => $organisation_id,
            'organisation_group_id' => $organisation_group_id,
            'organisation_member_id' => $organisation_member_id,
            'role' => $role,
            'active' => 1
        ));

        if( $saved ) {
            return array('status' => 'success', 'message' => 'Added leader successfully', 'leader' => $saved);
        }

        return array('status' => 'failed', 'message' => 'Failed to add leader to group due to database error');
    }
    
    private function getLeader($organisation_id, $organisation_group_id, $organisation_member_id) {
        return $this->OrganisationGroupLeader->find('first', array(
            'conditions' => array(
                'OrganisationGroupLeader.organisation_id' => $organisation_id,
                'OrganisationGroupLeader.organisation_group_id' => $organisation_group_id,
                'OrganisationGroupLeader.organisation_member_id' => $organisation_member_id
            ),
            'recursive' => -1
        ));
    }
    
    public function updateLeaderRole($organisation_id, $organisation_group_leader_id, $role) {
        $this->OrganisationGroupLeader->save(array(
            'id' => $organisation_group_leader_id,
            'organisation_id' => $organisation_id,
            'role' => $role
        ));
        
        return array('status' => 'success', 'message' => 'Leader role updated successfully');
    }
    
    public function removeLeader($organisation_id, $organisation_group_leader_id) {
        $this->OrganisationGroupLeader->save(array(
            'id' => $organisation_group_leader_id,
            'organisation_id' => $organisation_id,
            'active' => 0
        ));
        
        return array('status' => 'success', 'message' => 'Leader successfully removed');
    }
}
